package com.liuc.practice.ipersistent.test.dao;

import com.liuc.practice.ipersistent.io.Resources;
import com.liuc.practice.ipersistent.sqlSession.SqlSession;
import com.liuc.practice.ipersistent.sqlSession.SqlSessionFactory;
import com.liuc.practice.ipersistent.sqlSession.SqlSessionFactoryBuilder;
import com.liuc.practice.ipersistent.test.pojo.User;

import java.io.InputStream;
import java.util.List;

/**
 * 传统方式查询
 *
 * @Author Liu Chang
 * @Date 2020/12/13 9:08 下午
 */
public class UserDAOImpl implements IUserDAO {

    private SqlSession getSqlSession() throws Exception {
        InputStream input = Resources.getResourcesAsInputStream("sqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(input);
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.builder();
        return sqlSessionFactory.openSqlSession();
    }

    @Override
    public List<User> selectAll(String statementId) throws Exception {
        List<User> users = getSqlSession().selectList(statementId);
        return users;
    }

    @Override
    public User selectOne(String statementId, User user) throws Exception {

        return getSqlSession().selectOne(statementId, user);
    }

    @Override
    public void insert(String statementId, User user) throws Exception {

        getSqlSession().insert(statementId, user);
    }

    @Override
    public void update(String statementId, User user) throws Exception {

        getSqlSession().update(statementId, user);
    }

    @Override
    public void delete(String statementId, User user) throws Exception {

        getSqlSession().delete(statementId, user);
    }

}
